package com.ruoyi.btlfbreed.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.btlfbreed.domain.MyTbChickenHouses;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.btlfbreed.domain.TbChickenHouses;
import com.ruoyi.btlfbreed.service.ITbChickenHousesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * chickenHousesController
 * 
 * @author codeman
 * @date 2024-05-03
 */
@RestController
@RequestMapping("/btlfbreed/houses")
public class TbChickenHousesController extends BaseController
{
    @Autowired
    private ITbChickenHousesService tbChickenHousesService;
    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询chickenHouses列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbChickenHouses tbChickenHouses)
    {
        startPage();
        List<TbChickenHouses> list = tbChickenHousesService.selectTbChickenHousesList(tbChickenHouses);
        return getDataTable(list);
    }

    /**
     * 导出chickenHouses列表
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:export')")
    @Log(title = "chickenHouses", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbChickenHouses tbChickenHouses)
    {
        List<TbChickenHouses> list = tbChickenHousesService.selectTbChickenHousesList(tbChickenHouses);
        ExcelUtil<TbChickenHouses> util = new ExcelUtil<TbChickenHouses>(TbChickenHouses.class);
        util.exportExcel(response, list, "chickenHouses数据");
    }

    /**
     * 获取chickenHouses详细信息
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId)
    {
        return success(tbChickenHousesService.selectTbChickenHousesByHouseId(houseId));
    }

    /**
     * 新增chickenHouses
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:add')")
    @Log(title = "chickenHouses", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbChickenHouses tbChickenHouses)
    {
        tbChickenHouses.setUserId(getUserId());
        return toAjax(tbChickenHousesService.insertTbChickenHouses(tbChickenHouses));
    }

    /**
     * 修改chickenHouses
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:edit')")
    @Log(title = "chickenHouses", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbChickenHouses tbChickenHouses)
    {
        return toAjax(tbChickenHousesService.updateTbChickenHouses(tbChickenHouses));
    }

    /**
     * 删除chickenHouses
     */
    @PreAuthorize("@ss.hasPermi('btlfbreed:houses:remove')")
    @Log(title = "chickenHouses", businessType = BusinessType.DELETE)
	@DeleteMapping("/{houseIds}")
    public AjaxResult remove(@PathVariable Long[] houseIds)
    {
        return toAjax(tbChickenHousesService.deleteTbChickenHousesByHouseIds(houseIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        List<SysUser> users=new ArrayList<>();
        List<SysUser> newusers = new ArrayList<>();
        if (loginUser.getUserId()==1)
        {
            users = iSysUserService.selectUserAll();
            newusers=users;
        }
        else
        {
            users = iSysUserService.selectUserAll();

            for (SysUser item :
                users)
            {
                for (SysRole item2 :
                    roles)
                {
                    List<SysRole> collect = item.getRoles().stream().filter(p -> p.getRoleId() == item2.getRoleId()).collect(Collectors.toList());
                    if (collect != null && collect.size() > 0)
                    {
                        newusers.add(item);
                        break;
                    }

                }
            }
        }
        return success(newusers);
    }

}
